<?php
namespace app\lawcase\controller;

use app\base\BaseController;
use app\lawcase\model\Party as PartyModel;
use app\lawcase\validate\PartyCheck;
use think\facade\Request;

class Party extends BaseController
{
    protected $model;

    public function __construct()
    {
        parent::__construct();
        $this->model = new PartyModel();
    }

    /**
     * 当事人列表
     */
    public function index()
    {
        if (Request::isAjax()) {
            $param = get_params();
            $where = [];

            // 增强搜索条件
            if (!empty($param['keywords'])) {
                $keywords = trim($param['keywords']);
                $where[] = ['p.name', 'like', '%' . $keywords . '%'];
            }

            // 分页参数处理
            $page = isset($param['page']) ? intval($param['page']) : 1;
            $limit = isset($param['limit']) ? intval($param['limit']) : 10;
            $param['page'] = $page;
            $param['limit'] = $limit;

            // 获取列表数据
            $list = $this->model->getList($where, $param);

            // 返回统一格式数据
            return json([
                'code' => 0,
                'msg' => '',
                'count' => $list['count'] ?? 0,
                'data' => $list['data'] ?? []
            ]);
        }
        return view();
    }

    /**
     * 添加/编辑当事人
     */
    public function add()
    {
        if (Request::isAjax()) {
            $param = get_params();

            // 验证数据
            try {
                validate(PartyCheck::class)->scene('add')->check($param);
            } catch (\Exception $e) {
                return json(['code' => 1, 'msg' => $e->getMessage()]);
            }

            try {
                if (isset($param['id']) && $param['id']) {
                    $result = $this->model->edit($param);
                } else {
                    $result = $this->model->add($param);
                }
                return json(['code' => 0, 'msg' => '操作成功', 'data' => ['id' => $result]]);
            } catch (\Exception $e) {
                return json(['code' => 1, 'msg' => '操作失败：' . $e->getMessage()]);
            }
        } else {
            $id = input('id', 0);
            // 已确保在add方法中正确初始化$detail数组
            $detail = $id ? $this->model->getDetail($id) : ['party_type' => 1, 'pid' => 0, 'cid' => 0, 'did' => 0];
            return view('', ['id' => $id, 'detail' => $detail]);
        }
    }

    /**
     * 删除当事人
     */
    public function delete()
    {
        $id = get_params("id");
        try {
            $result = $this->model->remove($id);
            return json(['code' => 0, 'msg' => '删除成功']);
        } catch (\Exception $e) {
            return json(['code' => 1, 'msg' => '删除失败：' . $e->getMessage()]);
        }
    }

    /**
     * 选择当事人
     */
    public function select()
    {
        $param = input();
        $where = [];

        // 搜索条件处理
        if (!empty($param['keyword'])) {
            $where[] = ['name|id_number', 'like', '%' . $param['keyword'] . '%'];
        }

        // 获取列表数据
        $list = $this->model->getList($where, $param);

        return view('party/select', ['list' => $list]);
    }

    /**
     * 查看当事人详情
     */
    public function view()
    {
        $id = input('id');
        $detail = $this->model->getDetail($id);

        if (!$detail) {
            return json(['code' => 1, 'msg' => '当事人不存在']);
        }

        return view('party/view', ['id' => $id, 'detail' => $detail]);
    }
}